package com.guigu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.eduservice.entity.Equipment;
import com.guigu.eduservice.entity.EquipmentReservation;
import com.guigu.eduservice.entity.Order;
import com.guigu.eduservice.mapper.EquipmentReservationMapper;
import com.guigu.eduservice.service.EquipmentReservationService;
import com.guigu.eduservice.service.EquipmentService;
import com.guigu.eduservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class EquipmentReservationImpl extends ServiceImpl<EquipmentReservationMapper, EquipmentReservation> implements EquipmentReservationService {
    @Autowired
    private EquipmentReservationService equipmentReservationService;
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private OrderService orderService;
    @Override
    public List<EquipmentReservation> selectAllByUserId() {
        List<EquipmentReservation> list = equipmentReservationService.list();
        if (list==null){
            return new ArrayList<>();
        }
        //通过时间判断是否过期
        for (EquipmentReservation reservation : list) {
            if (reservation.getExpireTime().before(new Date())){
                //过期
                //根据elementId修改设备状态
                Long equipmentId = reservation.getEquipmentId();
                QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("equipment_id",equipmentId);
                //根据elementId修改设备状态
                Equipment equipment = new Equipment();
                equipment.setStatus(1);
                boolean update = equipmentService.update(equipment, queryWrapper);
                //根据设备id修改预定状态
                reservation.setReservationStatus(4);
                boolean update1 = equipmentReservationService.update(reservation, new QueryWrapper<EquipmentReservation>().eq("equipment_id", equipmentId));
                //根据elementId修改订单状态
                QueryWrapper<Order> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("equipment_id",equipmentId);
                Order order = new Order();
                order.setOrderStatus(7);
                boolean update2 = orderService.update(order, queryWrapper1);
            }
        }

        return list;
    }
}
